#include <stdio.h>
#include <stdlib.h>

typedef struct {
	void(*prin)(void* slef,const char* str);
}BASE;


typedef struct {
	BASE* user_prin;
}USER_CAR;


typedef struct{
	BASE car;
	const char* bwm_name;
}BWM;

typedef struct {
	BASE car;
	const char* benz_name;
}BENZ;

void bwm_print(void *self,const char* str) {
	BWM* bwm = (BWM*)self; 
	printf("%s is :%s\r\n",bwm->bwm_name,str);
}



void benz_print(void* self,const char* str){
	BENZ* benz = (BENZ*)self;
	printf("%s is %s...\r\n",benz->benz_name,str);
}


BWM* bwm_create(const char* str){
	BWM*  bwm = malloc(sizeof(BWM));
	bwm->car.prin = bwm_print;
	bwm->bwm_name = str;
	return bwm;
}


BENZ* benz_create(const char* str) {
	BENZ* benz = malloc(sizeof(BENZ));
	benz->car.prin = benz_print;
	benz->benz_name = str;
	return benz;
}


USER_CAR* user_register(BASE* base) {
	USER_CAR* user_car = malloc(sizeof(base));
	user_car->user_prin = base;
	return user_car;
}

void action_print(USER_CAR* user_car,const char* str) {
	printf("user_car is running...\r\n");
	user_car->user_prin->prin(user_car->user_prin,str);
	printf("car_end\r\n");
}


int main(){
	BWM* my_bwm = bwm_create("bwm");
	BENZ* my_benz = benz_create("benz");

	USER_CAR* user_benz = user_register((BASE*)my_benz);
	USER_CAR* user_bwm = user_register((BASE*)my_bwm);

	action_print(user_benz,"benz is left");
	action_print(user_bwm,"bwm is right");

	free(my_bwm);
	free(my_benz);

	free(user_benz);
	free(user_bwm);
	

	return 0;
}
